package cn.iocoder.yudao.module.wms.service.location;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.wms.controller.admin.location.vo.LocationPageReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.location.vo.LocationSaveReqVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.location.LocationDO;
import cn.iocoder.yudao.module.wms.dal.mysql.location.LocationMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wms.enums.ErrorCodeConstants.LOCATION_EXISTS;
import static cn.iocoder.yudao.module.wms.enums.ErrorCodeConstants.LOCATION_NOT_EXISTS;


/**
 * 货位档案 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class LocationServiceImpl implements LocationService {

    @Resource
    private LocationMapper locationMapper;

    @Override
    public Long createLocation(LocationSaveReqVO createReqVO) {

        // 查询编码是否存在
        validateCodeExists(createReqVO.getCode());
        // 插入
        LocationDO location = BeanUtils.toBean(createReqVO, LocationDO.class);
        locationMapper.insert(location);
        // 返回
        return location.getId();
    }

    @Override
    public void updateLocation(LocationSaveReqVO updateReqVO) {
        // 校验存在
        LocationDO locationDO = validateLocationExists(updateReqVO.getId());

        // 判断是否更新了编码
        if (!Objects.equals(locationDO.getCode(), updateReqVO.getCode())) {
            // 校验编码是否存在
            validateCodeExists(updateReqVO.getCode());
        }
        // 更新
        LocationDO updateObj = BeanUtils.toBean(updateReqVO, LocationDO.class);
        locationMapper.updateById(updateObj);
    }

    @Override
    public void deleteLocation(Long id) {
        // 校验存在
        validateLocationExists(id);
        // 删除
        locationMapper.deleteById(id);
    }
    private void validateCodeExists(String code) {
        LambdaQueryWrapper<LocationDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LocationDO::getCode, code);
        LocationDO locationDO = locationMapper.selectOne(queryWrapper);
        if (locationDO!=null) {
            throw exception(LOCATION_EXISTS);
        }
    }
    private LocationDO validateLocationExists(Long id) {
        if (locationMapper.selectById(id) == null) {
            throw exception(LOCATION_NOT_EXISTS);
        }

        return locationMapper.selectById(id);
    }

    @Override
    public LocationDO getLocation(Long id) {
        return locationMapper.selectById(id);
    }

    @Override
    public PageResult<LocationDO> getLocationPage(LocationPageReqVO pageReqVO) {
        return locationMapper.selectPage(pageReqVO);
    }

    @Override
    public List<LocationDO> getLocationSimpleList() {
        return locationMapper.selectList();
    }

    @Override
    public List<LocationDO> getListByWarehouse(Long inventoryId, String warehouseId) {
        return locationMapper.selectListByWarehouse(inventoryId, warehouseId);
    }

}